/**
 * 初始化
 */
$(function () {
    init();
    // 初始化验证类型自动隐藏
    setType();
});

/**
 * 加载layui样式
 */
function init() {
    layui.use(['form', 'laydate', 'dtree'], function () {

        var form = layui.form
            , dtree = layui.dtree;

        form.on('radio(menuType)', function (data) {
            switch (data.value) {
                case 'M' :
                    $(".icon").css("display", "block");
                    $("#icon").attr("validate", "required");
                    $(".url,.perms").css("display", "none");
                    $("#url,#perms").removeAttr("validate");
                    $("#url").val("#");
                    $("#perms").val("");
                    break;
                case 'C' :
                    $("#url").val("");
                    $(".url,.perms,.icon").css("display", "block");
                    $("#url,#perms,#icon").attr("validate", "required");
                    break;
                case 'F' :
                    $(".perms").css("display", "block");
                    $("#perms").attr("validate", "required");
                    $(".url,.icon").css("display", "none");
                    $("#url,#icon").removeAttr("validate");
                    $("#url").val("#");
                    $("#icon").val("");
                    break;
            }
        })
    });
}

/**
 * 设置父级菜单
 */
function setParentId() {


    // 验证是否存在子类存在则不允许修改
    // 当前菜单Id
    var menuId = $("#menuId").val();
    var loading = "";
    $.ajax({
        url: prefixMenu + "/moveParentMenuVerify"
        , type: "post"
        , data: {
            // 当前部门id
            "menuId": menuId,
        }
        , beforeSend: function () {
            loading = layTool.toplayer.load(0, {shade: false});
        }
        , success: function (data) {
            layTool.toplayer.close(loading);
            var errcode = data.errcode;
            var errmsg = data.errmsg;
            if (errcode == "0") {
                // 父级菜单Id
                var parentId = $("#parentId").val();
                var parentName = $("#parentName").val();
                var title = "选择父级菜单信息",
                    url = prefixOpen + "/web!system!menu!menuTree!menuTree@parentId=" + parentId + "&parentName=" + parentName,
                    width = "20%",
                    height = "55%";
                //  打开新窗口
                layTool.openDialog(title, url, width, height, getParentDate);
            } else {
                layTool.showAlert(errmsg);
            }
        }
        , error: function () {
            layTool.toplayer.close(loading);
            AjaxErro();
        }
    })


}

/**
 * @Description: 获取父级菜单id和名称
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/20
 */
function getParentDate(index, layero) {
    var body = layTool.getChildFrame("body", index);
    $("#parentId").val(body.find('#parentId').val());
    $("#parentName").val(body.find('#parentName').val());
    layTool.closeLayer(index);
}

/**
 * 设置图标按钮
 */
function setIcon() {
    var icon = $("#icon").val();
    var title = "选择图标信息",
        url = prefixOpen + "/web!system!menu!icon!icon@icon=" + icon,
        width = "20%",
        height = "40%";
    //  打开新窗口
    layTool.openDialog(title, url, width, height, getIcon);
}

/**
 * @Description: 获取图标的名字
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/20
 */
function getIcon(index, layero) {
    var body = layTool.getChildFrame("body", index);
    $("#icon").val(body.find('#icon').val());
    layTool.closeLayer(index);
}

/**
 * @Description: 自动隐藏表单
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/24
 */

function setType() {
    var menuType = $("#menuType").val();
    switch (menuType) {
        case 'M' :
            $(".icon").css("display", "block");
            $("#icon").attr("validate", "required");
            $(".url,.perms").css("display", "none");
            $("#url,#perms").removeAttr("validate");
            $("#url,#perms").val("");
            break;
        case 'C' :
            $(".url,.perms,.icon").css("display", "block");
            $("#url,#perms,#icon").attr("validate", "required");
            break;
        case 'F' :
            $(".perms").css("display", "block");
            $("#perms").attr("validate", "required");
            $(".url,.icon").css("display", "none");
            $("#url,#icon").removeAttr("validate");
            $("#url,#icon").val("");
            break;
    }
}


